package org.example;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import org.keyvalue.KeyValueServiceOuterClass.*;
import org.keyvalue.KeyValueServiceGrpc;

public class KeyValueClient1 {
    public static void main(String[] args) throws InterruptedException {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 50051)
                .usePlaintext()
                .build();

        KeyValueServiceGrpc.KeyValueServiceBlockingStub stub = KeyValueServiceGrpc.newBlockingStub(channel);

        for(int i=0;i<1000;i++) {
            Thread.sleep(1000);
            String s1="myKey"+i;
            String s2="myValue"+i;
            // 使用 stub 调用 gRPC 服务的方法
            PutRequest putRequest = PutRequest.newBuilder().setKey(s1).setValue(s2).build();
            PutResponse putResponse = stub.put(putRequest);
            System.out.println("Put Response: " + putResponse.getSuccess()+" "+s1);
        }

        channel.shutdown();
    }
}
